/***********************
*DESC:场景配置
*Author:wff 
*Date:2015-05-8
*
************************/
function SetSceneGroupTableResult(gridId)
{
	var data=[];
	data = $.fn.tables.SetSceneGroupTableResult(gridId);
	return data;
}
function SetScenesTableResult(gridId)
{
	var data=[];
	data = $.fn.tables.SetScenesTableResult(gridId);
	return data;
}
function GetCurrSceneGroupData(curscesetid)
{
	var isINsert = false;
	if(CurrScenesArry.length == 0){
			
		return false;
	}
	else{
		
		var ers;
		$.each(CurrScenesArry,function(index,item){
			
			if(item.id == curscesetid){
				isINsert = true;
				var news = item.scene;
				if(typeof(news) == "object" && news.constructor == Array){
					ers = item.scene;
				}else{
					ers = [];
					ers.push(item.scene);
				}
				return false;
			}				
		})
		if(isINsert == false){
				
			return false;
		}
		else{
				
			return ers;
		}
	}
	
	
	
}
function InitSceneGroupTableData(curscesetid, cmdarry)
{	
	
	$.fn.tables.bindSceneGroupData('ScenesAddModalSceneGroupListtable', eval(cmdarry));
}
function GetScenceGroupFrom(curscesetid)
{
	$("#ScenesAddModalSceneGroupListtable > tbody > tr > td").eq(2).children("a").eq(0).attr("onClick","GetSceneFrom(this)");
	$('#ScenesAddModal').modal('show');
	$('#curscesetid').val(curscesetid);
	$('#SceneGroupAddDiv').hide();
	$('#ScenesAddModal .input-small').removeClass("error");
	$('#ScenesAddModal .text-error').remove();
	$("#ScenesAddModal_addbtn").attr("onclick","AddScenesAction('"+curscesetid+"');");
	
	var cmddata = GetCurrSceneGroupData(curscesetid);
	 cmddata.sort(
                function(a, b)  
                {  
                    var aa = parseFloat(a.id);
					var bb = parseFloat(b.id);
					 
					if(aa < bb) 
						return -1;  
                    else if(aa > bb) 
						return 1;  
                    else 
						return 0;  
                }  
            ); 
	
	if(cmddata[0] == undefined){
		cmddata[0]=[];
	}
	InitSceneGroupTableData(curscesetid, cmddata);
	if(cmddata[0].length==0){
		$("#ScenesAddModalSceneGroupListtable > tbody > tr > td").eq(2).children("a").eq(0).attr("onClick","");
	}
}

function EditSceneGroupAction(curscesetid)
{
	 scesetint ++;
	 var currscenFloorid = $( '#scenFloorid' ).val();
	 var currscenAreaid = $( '#scenAreaid' ).val();
	 var currscenFloorname = $( '#scenFloorid' ).find("option:selected").text();
	 var currscenAreaname = $( '#scenAreaid' ).find("option:selected").text();
	 var name = $( '#scenname' ).val();
	 var tab = new Array();
	 tab = getRowResult('SceneGroupListtable');
	 
	if(CurrSceneGroupArry.length != 0){	 		  
		 $.each(CurrSceneGroupArry,function(index,item){
			if(item.id == curscesetid){				
				CurrSceneGroupArry.splice(index, 1);
				return false;
			}				
		 })
		 
		  CurrSceneGroupArry.push({"id": scesetint,"floorid": currscenFloorid,"floorname": currscenFloorname,"aredid": currscenAreaid,"aredname": currscenAreaname,"name": name, "scene": getRowResult('SceneGroupListtable')});
	  }
	  else
	  	  CurrSceneGroupArry.push({"id": scesetint,"floorid": currscenFloorid,"floorname": currscenFloorname,"aredid": currscenAreaid,"aredname": currscenAreaname,"name": name, "scene": getRowResult('SceneGroupListtable')});
	 $('#ScenesListTable').bootstrapTable('load',{data:CurrSceneGroupArry});
		
	
}
function AddSceneGroupAction()
{
	 
	 var currscenFloorid = $( '#scenFloorid' ).val();
	 var currscenAreaid = $( '#scenAreaid' ).val();
	 var currscenFloorname = $( '#scenFloorid' ).find("option:selected").text();
	 var currscenAreaname = $( '#scenAreaid' ).find("option:selected").text();
	 var name = $( '#scenname' ).val();
	 if(currscenFloorid == "" || currscenFloorid == "null" || currscenFloorid == null)
	 {
		 
		 $('#scenFloorid').addClass('error').after("<span class='text-error'>请选择楼层</span>");
		return;
	 }
	 if(currscenAreaid == "" || currscenAreaid == "null" || currscenAreaid == null)
	 {
		 
		 $('#scenAreaid').addClass('error').after("<span class='text-error'>请选择区域</span>");
		return;
	 }
	 var ispass = validateRow('SceneGroupaddTable');
	if(!ispass){
			return;
	}
	var Tab = SetSceneGroupTableResult('SceneGroupListtable');
	if(Tab == 0){
		$('#slistfont').after("<span class='text-error'>请添加子场景类型</span>");
		return;
	}
	
	//对Tab数组进行倒序操作
	var TabArry = new Array();
	for (var i = 0; i < Tab.length; i++) {
		TabArry.push(Tab[Tab.length-i-1]);
	}
	Tab = TabArry; 
	
	 var isAlreadyname = false;
	 if(CurrScenesArry.length != 0){
		
		var Curname = [];
		Curname = $.grep(CurrScenesArry,function(item, index){
													   
			if(item.name == name)
				return true;
		},false);
		if(Curname.length != 0)
			isAlreadyname = true;	 	
	 }
	 if(isAlreadyname == true)
	 {
		var errorMsg = "该场景组名称已经存在";
		$('#scenname').addClass('error').after("<span class='text-error'>" + errorMsg + "</span>");
		return;
	 }
	scesetint ++;
	 CurrScenesArry.push({"id": scesetint,"floorid": currscenFloorid,"floorname": currscenFloorname,"aredid": currscenAreaid,"aredname": currscenAreaname,"name": name, "scene": Tab});
	 $('#ScenesListTable').bootstrapTable('load',{data:CurrScenesArry});
	 $('#SceneGroupAddModal').modal('hide'); 
	
	
}
function GetCurrScesetScenesData(curscesetid,cursceneTypeid)
{
		var isHave = false;
		var ers;
		var tbResult = new Array;
	$.each(CurrScenesArry,function(index,item){
		if(item.hasOwnProperty("scene")&&item.scene!=undefined){	
			if(item.id == curscesetid){
				var NewSences = item.scene;
				if(typeof(NewSences) == "object" && NewSences.constructor == Array){				
					$.each(item.scene,function(n,value){
						
						var json = value;
						
						if(value.id == cursceneTypeid){
							if(value.hasOwnProperty("i")){
								if(value.i == 0){
										console.log("meiyI");
								}else{
										isFind = true;
										isHave = true;
										var NewSencesI = value.i;
										if(typeof(NewSencesI) == "object" && NewSencesI.constructor == Array){
										
											ers=value.i;
											return false;
										}else{
											ers = [];
											ers.push(NewSencesI);
										}
								}
							}
						}
						
					})
				}else{
					if(NewSences.id == cursceneTypeid){
							if(NewSences.hasOwnProperty("i")){
								if(NewSences.i == 0){
										console.log("meiyI");
								}else{
										isFind = true;
										isHave = true;
										var NewSencesI = NewSences.i;
										if(typeof(NewSencesI) == "object" && NewSencesI.constructor == Array){
										
											ers=NewSences.i;
											
										}else{
											ers = [];
											ers.push(NewSencesI);
										}
								}
							}
						}
				}
				
				return false;
				
			}
			if(isHave == true)
				return false;
		}
		else{
			ers = [];
			return ers;
		}
	})
	if(isHave == false)
		return false;
	else
		return ers;
}
function InitScenesTableData(cursceneTypeid, cursceneTypename, cmddata)
{
	$( '#hsceneTypename' ).val(cursceneTypename);
	$.fn.tables.bindSceneGroupData('ScenesAddtable', eval(cmddata));
}
function InitScenesForm()
{
		var trHtml = $("#ScenesAddtable").find("tr").slice(1).first().html();
		$("#ScenesAddtable").find("tbody").find("tr").remove();
		$("#ScenesAddtable").append("<tr>" + trHtml + "</tr>");
		/*********add************/
		var $floorid = $("#ScenesAddtable" + " #floorid");
        var $e_epid = $("#ScenesAddtable" + " #epid");
		var $oid = $("#ScenesAddtable" + " #oid");
		var $val = $("#ScenesAddtable" + " #val");
		InitFloorOption($floorid);
}
function GetSceneFrom(obj)
{	
	$.fn.tables.GetSceneFrom(obj);
}
function DelSonSceneFrom(obj)
{
	$("#SceneGroupAddDiv").hide();
	var CurScesetId = $("#curscesetid").val();
	var CurTrObj = $(obj).parent("td").parent("tr");
	var AllTrObj = $("#ScenesAddModalSceneGroupListtable > tbody >tr");
	if(AllTrObj.length > 1){
		CurTrObj.remove();
	}else{
		CurTrObj.remove();
		var trhtml = '<tr >'+
							'<td><input type="text"  class="input-small" name="id" id="id"  readonly="true" ></td>'+
							'<td><input type="text"  class="input-small" name="name" id="name" readonly ></td>'+
								
							'<td align="center">'+
                                '&nbsp&nbsp&nbsp'+
                                '<a  title="子场景内容配置" onClick=""><i class="icon-edit"></i></a>'+
                                '&nbsp&nbsp&nbsp'+
                                '<a  title="子场景内容删除" onClick="DelSonSceneFrom(this)"><i class="icon-remove"></i></a>'+
                            '</td>'+			
							
						'</tr>';
		$("#ScenesAddModalSceneGroupListtable > tbody").append(trhtml);
	}
}
function AddScenesAction(curscesetid, cursceneTypeid)
{
	var SonScenes = $.fn.tables.SetSceneGroupTableResult("ScenesAddModalSceneGroupListtable");
	$.each(CurrScenesArry,function(indexson,itemson){
		if(itemson.id == curscesetid){
			itemson.scene = SonScenes;
			if(itemson.scene == 0){
				delete itemson.scene;
			}
		}
	})
	var isHave = false;
	var Tab = SetScenesTableResult('ScenesAddtable');
	if(Tab != 0)
	{
		var ispass = validateRow('ScenesAddtable');
			if(!ispass){
				$.formSmallInit();
				return;
			}
	}
	//对Tab数组进行倒序操作
	var TabArry = new Array();
	for (var i = 0; i < Tab.length; i++) {
		TabArry.push(Tab[Tab.length-i-1]);
	}
	Tab = TabArry; 
	
	$.each(CurrScenesArry,function(index,item){
			
			if(item.id == curscesetid){
				var floorid = item.floorid;
				var aredid = item.aredid;
				var name = item.name;
				if(item.scene != undefined){
					var NewSences = item.scene;
					if(typeof(NewSences) == "object" && NewSences.constructor == Array){
						$.each(item.scene,function(n,value){
							if(value.id == cursceneTypeid){						
											
								isFind = true;
								isHave = true;				
								item.scene.splice(n, 1);						
								item.scene.push({"id": cursceneTypeid,"name": sceneTypeMap.get(cursceneTypeid),"i": Tab});						
								return false;
							}
						})
					}else{
						if(NewSences.id == cursceneTypeid){						
											
								isFind = true;
								isHave = true;
								var news = [];
								news.push({"id": cursceneTypeid,"name": sceneTypeMap.get(cursceneTypeid),"i": Tab});
								item.scene = news;
								//item.scene.splice(n, 1);						
								//item.scene.push({"id": cursceneTypeid,"name": sceneTypeMap.get(cursceneTypeid),"i": SetScenesTableResult('ScenesAddtable')});						
								//return false;
							}
					}
					return false;
				}
			}				
	})	 	
//console.log("CurrScenesArry=" + JSON.stringify(CurrScenesArry));
 $('#ScenesListTable').bootstrapTable('load',{data:CurrScenesArry});
	
}
function DelScesetAction(currDelIds)
{
	var Delrules = currDelIds.split(",");
	
	for (var i = 0; i < Delrules.length; i++) {
	
		var curscesetid = Delrules[i];
		
		$.each(CurrScenesArry,function(index,item){
			if(item.id == curscesetid){
				CurrScenesArry.splice(index, 1);
				return false;
			}
		})
	}
	$("#DeleteAllModal_delbtn").removeAttr("onclick");
	$('#DeleteAllModal').modal('hide');
	$('#ScenesListTable').bootstrapTable('load',{data:CurrScenesArry});
	
}
function InitSenceTable()
{
	$('#ScenesListTable').bootstrapTable({							
	data: CurrScenesArry,
	//height: 405,
	striped: true,
	dataType: "json",
	pagination: true,	
	singleSelect: false,	
	contentType: 'application/json',
	pageSize: 5,
	pageNumber:1,
	pageList: [5,10, 25, 50, 100],
	showRefresh: false,
	showToggle: false,	
	search: false, //不显示 搜索框
	showColumns: false, //不显示下拉框（选择显示的列）
	sidePagination: "client", //服务端请求
	columns: [
		{
                    field: 'state',
                    checkbox: true
         }, 	
		{
			field: 'id',	
			title: '场景组ID',	
			width: '10%',		
			align: 'center',	
			valign: 'middle',	
			sortable: false	
		},
		{
			field: 'name',	
			title: '场景组名称',	
			width: '20%',		
			align: 'center',	
			valign: 'middle',	
			sortable: false	
		},
		{
			field: 'aredid',	
			title: '场景组位置',	
			width: '20%',		
			align: 'center',	
			valign: 'middle',
			formatter: ScenceAreaInfoFormatter,	
			sortable: false	
		},
		
		{
			field: 'atype',	
			title: '子场景信息',	
			width: '30%',	
			align: 'center',	
			valign: 'middle',
			formatter: ScenceGroupInfoFormatter,
			sortable: false	
		},
		
		{
			title: '场景组管理',
			field: 'operate',	
			width: '20%',	
			align: 'center',	
			valign: 'middle',	
			formatter: SenceViewFormatter,				
			sortable: false	
		}
	]
	
});
}
function ScenceAreaInfoFormatter(value, row, index) {
            return ['所属楼层：'+ row.floorname + '<br>所属区域：'+row.aredname
                    ].join('');
}
function ScenceGroupInfoFormatter(value, row, index) {

            return [JSON.stringify(row.scene)
                    ].join('');
}
function SenceViewFormatter(value, row, index) {
return '<a class="btn-flat gray" style="text-decoration:none" onclick="GetScenceGroupFrom(\''+row.id+'\')">场景组管理</a>';

}